﻿using System;
using System.Collections.Generic;
using System.Text;

using Obsidian.Action;
using Oak.Base;
using Oak.Model;
using Oak.MagSys.Logic;

namespace Oak.MagSys.Serv
{
    public class MenuItemServ : OakServ<ISession>
    {

        private const string FN_MENU_ITEM = "itemId,parentId,frameId,text,isSystemMenuItem,sortOrder";

        public MenuItemServ(OakSession session) : base(session) { }

        public void List(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            MsMenuItemQry qry = req.FirstModel<MsMenuItemQry>();
            string msg;
            MenuItemLG itemLG = new MenuItemLG(Session);
            List<MsMenuItemInfo> list = itemLG.GetMenuItemList(qry, out msg);
            if (list == null)
            {
                res.Error(msg);
                return;
            }
            res.AddResult("items", FN_MENU_ITEM, list);
        }

        public void Add(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            MsMenuItemInfo menuItem = req.FirstModel<MsMenuItemInfo>();
            string msg;
            MenuItemLG itemLG = new MenuItemLG(Session);
            menuItem = itemLG.AddMenuItem(menuItem, out msg);
            if (menuItem == null)
            {
                res.Error(msg);
                return;
            }

            res.AddResult("items", FN_MENU_ITEM, menuItem);
        }

        public void Update(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            MsMenuItemInfo item = req.FirstModel<MsMenuItemInfo>();
            string msg;
            MenuItemLG itemLG = new MenuItemLG(Session);
            item = itemLG.UpdateMenuItem(item, out msg);
            if (item == null)
            {
                res.Error(msg);
                return;
            }
            res.AddResult("items", FN_MENU_ITEM, item);
        }

        public void Delete(ActionRequest req, ActionResponse res)
        {
            if (!Session.IsAdmin)
            {
                res.SessionInvalid();
                return;
            }
            long menuItemId;
            if (!req.TryGetLong("itemId", out menuItemId))
            {
                res.Error("itemId参数错误");
                return;
            }

            string msg;
            MenuItemLG itemLG = new MenuItemLG(Session);
            if (!itemLG.DeleteMenuItem(menuItemId, out msg))
            {
                res.Error(msg);
                return;
            }
        }
    }
}
